package com.massyframework.beanskin.license.signature.wizard;

import com.massyframework.beanskin.util.version.VersionRange;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class ModuleEditorController {
	
	private Module module;
	private Stage stage;
	
	@FXML
	private TextField txtGroupId;
	@FXML
	private TextField txtModuleId;
	@FXML
	private TextField txtVersionRange;
	@FXML
	private TextField txtAlias;
	
	@FXML
	private TableView<Parameter> tvParams;
	@FXML
	private TableColumn<Parameter, String> tcName;
	@FXML
	private TableColumn<Parameter, String> tcValue;
	@FXML
	private TableColumn<Parameter, String> tcAction;
	
	@FXML
	private DatePicker dpExpire;
	
	@FXML
	private TextField txtName;
	@FXML
	private TextField txtValue;
	
	private ObservableList<Parameter> list;
	

	public ModuleEditorController() {
	}
	
	/**
	 * 初始化
	 * @param module
	 */
	public void init(Stage stage, Module module) {
		this.stage = stage;
		this.module = module;
		
		this.txtGroupId.setText(module.getGroupId());
		this.txtGroupId.setEditable(false);
		this.txtModuleId.setText(module.getModuleId());
		this.txtModuleId.setEditable(false);
		
		this.txtVersionRange.setText(module.getVersionRange().toString());	
		
		this.txtAlias.setText(module.getCertAlias());
		this.txtAlias.setEditable(false);
		
		if (module.getExpireDate() != null) {
			this.dpExpire.setValue(module.getExpireDate());
		}
		
		this.list = FXCollections.observableArrayList();
		this.list.addAll(module.getParameters());
		this.tvParams.setItems(this.list);
		this.tcName.setCellValueFactory(new PropertyValueFactory<Parameter, String>("name"));		
		this.tcValue.setCellValueFactory(new PropertyValueFactory<Parameter, String>("value"));
		
		this.tcAction.setCellFactory((col) -> {
			TableCell<Parameter, String> cell = new TableCell<Parameter, String>() {

				@Override
				public void updateItem(String item, boolean empty) {
					super.updateItem(item, empty);
					this.setText(null);
					this.setGraphic(null);

					if (!empty) {
						Button btnEdit = new Button("删除");
						Font font = Font.font(12);
						btnEdit.setFont(font);

						this.setAlignment(Pos.CENTER);
						this.setGraphic(btnEdit);
						btnEdit.setOnMouseClicked((me) -> {
							Parameter parameter = this.getTableView().getItems().get(this.getIndex());
							list.remove(parameter);
						});
					}
				}

			};
			return cell;
		});
	}
	
	@FXML
	protected void onAddClick(ActionEvent event) {
		if (this.txtName.getText().trim().length() >0 &&
				this.txtValue.getText().trim().length() > 0) {
			String name = this.txtName.getText().trim();
			boolean exists = this.list.stream().anyMatch(p-> 
				p.getName().equals(name)
			);
			
			if (exists) {
				Alert alert = new Alert(AlertType.WARNING);
				alert.setTitle("提示");
				alert.setHeaderText("参数已经存在");
				alert.setContentText("\"".concat(name).concat("\" 以经存在，不能重复设置."));
				alert.showAndWait();
				
				this.txtName.clear();
				this.txtValue.clear();
				return;
			}
			
			Parameter parameter = new Parameter();
			parameter.setName(name);
			parameter.setValue(this.txtValue.getText().trim());
		
			this.list.add(parameter);
			
			this.txtName.clear();
			this.txtValue.clear();
		}
	}
	
	@FXML
	protected void onOkClick(ActionEvent event) {
		String text = this.txtVersionRange.getText().trim();
		try {
			VersionRange vr = VersionRange.valueOf(text);
			this.module.setVersionRange(vr);
			
			this.module.setExpireDate(dpExpire.getValue());
		}catch(Exception e) {
			Alert alert = new Alert(AlertType.WARNING);
			alert.setTitle("提示");
			alert.setHeaderText("版本范围的格式不对");
			alert.setContentText("\"".concat(text).concat("\" 不是正确的版本范围格式."));
			alert.showAndWait();
			
			this.txtVersionRange.setText(this.module.getVersionRange().toString());
			return;
		}
		
		this.module.getParameters().clear();
		this.module.getParameters().addAll(this.list);
		System.out.println(module.getParameters().size());
		this.stage.close();
	}
	

}
